﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using SystemDiagramNew.TypeDefinition;

namespace SystemDiagramNew.Utils
{
	public static class ExternalCommandDataExtension
	{
		public static TextNoteType CreateTextNoteType(this ExternalCommandData revit)
		{
			return revit.CreateTextNoteType(2.5, 0.7, "宋体");
		}

		public static TextNoteType CreateTextNoteType(this ExternalCommandData revit, double textHeight, double widthFactor, string fontName)
		{
			double num = textHeight.MMtoFeet();
			string newName = textHeight.ToString("F2") + " mm " + fontName;
			IEnumerable<TextNoteType> elements = revit.GetDocument().GetElements<TextNoteType>();
			if (!elements.Any<TextNoteType>())
			{
				return null;
			}
			TextNoteType textNoteType = elements.FirstOrDefault((TextNoteType t) => t.Name == newName) ?? elements.First<TextNoteType>();
			if (textNoteType == null)
			{
				return null;
			}
			Parameter parameter = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE);
			if (parameter == null)
			{
				return textNoteType;
			}
			double val = parameter.AsDouble();
			Parameter parameter2 = textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE);
			if (parameter2 == null)
			{
				return textNoteType;
			}
			double val2 = parameter2.AsDouble();
			if (Geometry.IsEqual(num, val, 0.001) && Geometry.IsEqual(widthFactor, val2, 0.001))
			{
				return textNoteType;
			}
			TextNoteType textNoteType2 = textNoteType.Duplicate(newName) as TextNoteType;
			if (textNoteType2 == null)
			{
				return null;
			}
			textNoteType2.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(num);
			textNoteType2.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).Set(widthFactor);
			textNoteType2.get_Parameter(BuiltInParameter.LEADER_OFFSET_SHEET).Set(0.0);
			return textNoteType2;
		}
	}
}
